<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
// 
//  login.php
//  www.6block.com
//  
//  Created by Will_Lee on 2011-05-17.
//  Copyright 2011 lishuzu. All rights reserved.
// 
class Login extends CI_Controller {
	function __construct(){
		parent::__construct();
		$this->load->model('User_model');
		$this->load->model('guest_model');
	}
	
	function sina_login(){
		//sina微博接入
		$this->load->helper('sinaweibo/sina_weibooauth');
		$weibouser = array();
		$sina_keys = $this->session->userdata('sina_keys');
//		var_dump($this->session->userdata('sina_keys'));
		$sina_login = new WeiboOAuth( SINA_WB_AKEY , SINA_WB_SKEY , $sina_keys['oauth_token'] , $sina_keys['oauth_token_secret']  );
		$last_key = $sina_login->getAccessToken($_REQUEST['oauth_verifier']);
		$sina_keys['last_key'] = $last_key;
		$sina_client = new WeiboClient( SINA_WB_AKEY , SINA_WB_SKEY , $sina_keys['last_key']['oauth_token'] , $sina_keys['last_key']['oauth_token_secret']  );
		$userinfo = $sina_client->verify_credentials();
		if (!$userinfo['error_code']) {
			$this->session->sess_destroy();
			$weibo_user = array(
					'id' => $userinfo['id'],
					'weibo_url' => SINA_WB_URL.$userinfo['id'],
					'name' => $userinfo['name'],
				);
			/*************访客*****************/
			$guestinfo = array(
					'name' => $weibo_user['name'],
					'avatar' => $userinfo['profile_image_url'],
					'from' => 'sina',
					'sina_uid' => $weibo_user['id'],
					'weibo_url' => $weibo_user['weibo_url'],
					'dateline' => now(),
					'lastlogin' => now(),
				);
			if ($guest = $this->guest_model->get_guest_where(array('sina_uid' => $guestinfo['sina_uid']))) {
				$updateguest = array(
					'guestid' => $guest['guestid'],
					'lastlogin' => now(),
					'name' => $weibo_user['name'],
					);
				$this->guest_model->update_guest($updateguest);
			}else{
				$this->guest_model->insert_guest($guestinfo);
			}
			
			//查找用户是已经存在
			$wheresql = array('sina_uid' => $userinfo['id'], 'from'=>'sina');
			$this->db->where($wheresql);
			$query = $this->db->get('user');
			$localuser = $query->row_array();
			if (empty($localuser)) {
				$insertdata = array(
					'username' => $userinfo['name'],
					'from' => 'sina',
					'lastlogin' => now(),
					'sina_uid' => $userinfo['id'],
					'avatar' => $userinfo['profile_image_url'],
					'weibo_url' => $weibo_user['weibo_url'],
					'isbind' => '0',
					'binduid' => '0',
					'dateline' => now(),
					);
				$insert_id = $this->User_model->insert_user($insertdata);
				if ($insert_id) {
					//加载登录信息
					$localuser = $this->User_model->get_user($insert_id);
					$session_data = array(
						'sina_keys' => $sina_keys,
						'weibo_user' => $weibo_user,
						'userinfo' => $localuser,
						);
					
					$this->session->set_userdata($session_data);
					
					showmessage('登录成功', base_url().'index');
				}else{
					showmessage('登录失败', base_url().'index');
				}
			}else{
				//更新用户信息				
				$updatedata = array(
					'lastlogin' => now(),
					'avatar' => $userinfo['profile_image_url'],
					'username' => $weibo_user['name'],
					);
				$this->User_model->update_userinfo_by_uid($localuser['uid'], $updatedata);
				//加载登录信息
				$session_data = array(
					'sina_keys' => $sina_keys,
					'weibo_user' => $weibo_user,
					'userinfo' => $localuser,
					);
				$this->session->set_userdata($session_data);
				showmessage('登录成功', base_url().'index');
			}
		}else{
			showmessage('登录失败', base_url().'index');			
		}
	}

	function qq_login(){
		$weibouser = array();
		$qq_keys = $this->session->userdata('qq_keys');
		$this->load->helper('qqweibo/opent');
		$qq_login = new MBOpenTOAuth( QQ_WB_AKEY, QQ_WB_SKEY, $qq_keys['oauth_token'], $qq_keys['oauth_token_secret']);
		$last_key = $qq_login->getAccessToken($_REQUEST['oauth_verifier']) ;//获取ACCESSTOKEN
		$qq_keys['last_key'] = $last_key;
		$this->load->helper('qqweibo/api_client');
		$qq_client = new MBApiClient( QQ_WB_AKEY , QQ_WB_SKEY , $last_key['oauth_token'] , $last_key['oauth_token_secret']  );
		//本人信息
		$userinfo = $qq_client->getUserInfo();
		if (!$userinfo['errcode']) {
			//微博头像
			if ($userinfo['data']['head']) {
				$userinfo['data']['head'] = $userinfo['data']['head'].'/100';
			}
			
			$this->session->sess_destroy();
			$weibo_user = array(
					'id' => $userinfo['data']['name'],
					'weibo_url' => QQ_WB_URL.$userinfo['data']['name'],
					'name' => $userinfo['data']['name'],
				);
				
			/*************访客*****************/
			$guestinfo = array(
					'name' => $weibo_user['name'],
					'avatar' => $userinfo['data']['head'],
					'from' => 'qq',
					'qq_uid' => $weibo_user['id'],
					'weibo_url' => $weibo_user['weibo_url'],
					'dateline' => now(),
					'lastlogin' => now(),
				);
			if ($guest = $this->guest_model->get_guest_where(array('qq_uid' => $guestinfo['qq_uid']))) {
				$updateguest = array(
					'guestid' => $guest['guestid'],
					'lastlogin' => now(),
					'name' => $weibo_user['name'],
					);
				$this->guest_model->update_guest($updateguest);
			}else{
				$this->guest_model->insert_guest($guestinfo);
			}
			//查找用户是已经存在
			$wheresql = array('qq_uid' => $userinfo['data']['name'], 'from'=>'qq');
			$this->db->where($wheresql);
			$query = @$this->db->get('user');
			$localuser = $query->row_array();
			if (empty($localuser)) {
				$insertdata = array(
					'username' => $userinfo['data']['name'],
					'from' => 'qq',
					'lastlogin' => now(),
					'qq_uid' => $userinfo['data']['name'],
					'avatar' => $userinfo['data']['head'],
					'weibo_url' => $weibo_user['weibo_url'],
					'isbind' => '0',
					'binduid' => '0',
					'dateline' => now(),
					);
				$insert_id = $this->User_model->insert_user($insertdata);
				if ($insert_id) {
					//加载登录信息
					$localuser = $this->User_model->get_user($insert_id);
					$session_data = array(
						'qq_keys' => $qq_keys,
						'weibo_user' => $weibo_user,
						'userinfo' => $localuser,
						);
					
					$this->session->set_userdata($session_data);
					
					showmessage('登录成功', base_url().'index');
				}else{
					showmessage('登录失败', base_url().'index');
				}
			}else{
				//更新用户信息
				$updatedata = array(
					'lastlogin' => now(),
					'avatar' => $userinfo['data']['head'],
					'username' => $weibo_user['name'],
					);
				$this->User_model->update_userinfo_by_uid($localuser['uid'], $updatedata);
				//加载登录信息
				$session_data = array(
					'qq_keys' => $qq_keys,
					'weibo_user' => $weibo_user,
					'userinfo' => $localuser,
					);
				$this->session->set_userdata($session_data);
				showmessage('登录成功', base_url().'index');
			}
		}else{
			showmessage('登录失败', base_url().'index');			
		}
	}

//开心网登录 kaixin001
	function kaixin_login(){
		$weibouser = array();
		//开心
		//开心配置
		$kxconfig = array();
		$kaixin001_session_key = get_cookie('kx_connect_session_key');
		$this->load->helper('kaixin001/kaixin001');
		$kxconfig = $this->config->item('kaixin001_wb');
		$kxconfig['session_key'] = $kaixin001_session_key;
		$kaixin_api = new KxPlatform_Api($kxconfig);
		$res = $kaixin_api->getLoggedInUser();
		if (!empty($res)) {
			$kaixin_uid = $res ? $res : '';
		}
		$kaixinuserinfo = $kaixin_api->getUserInfo($kaixin_uid);
		//本人信息
		$userinfo = $kaixinuserinfo[0];
		if ($userinfo['name']) {
			$this->session->sess_destroy();
			//重新种cookie
			set_cookie("kx_session_key", $kaixin001_session_key, time()+3600*6);
			
			$weibo_user = array(
					'id' => $userinfo['uid'],
					'weibo_url' => KX_WB_URL.'home/?uid='.$userinfo['uid'],
					'name' => $userinfo['name'],
				);
			/*************访客*****************/
			$guestinfo = array(
					'name' => $weibo_user['name'],
					'avatar' => $userinfo['logo50'],
					'from' => 'kaixin',
					'kaixin_uid' => $weibo_user['id'],
					'weibo_url' => $weibo_user['weibo_url'],
					'dateline' => now(),
					'lastlogin' => now(),
				);
			if ($guest = $this->guest_model->get_guest_where(array('kaixin_uid' => $guestinfo['kaixin_uid']))) {
				$updateguest = array(
					'guestid' => $guest['guestid'],
					'lastlogin' => now(),
					'name' => $weibo_user['name'],
					);
				$this->guest_model->update_guest($updateguest);
			}else{
				$this->guest_model->insert_guest($guestinfo);
			}
			//查找用户是已经存在
			$wheresql = array('kaixin_uid' => $userinfo['uid'], 'from'=>'kaixin');
			$this->db->where($wheresql);
			$query = $this->db->get('user');
			$localuser = $query->row_array();
			if (empty($localuser)) {
				$insertdata = array(
					'username' => $userinfo['name'],
					'from' => 'kaixin',
					'lastlogin' => now(),
					'kaixin_uid' => $userinfo['uid'],
					'avatar' => $userinfo['logo50'],
					'weibo_url' => $weibo_user['weibo_url'],
					'isbind' => '0',
					'binduid' => '0',
					'dateline' => now(),
					);
				$insert_id = $this->User_model->insert_user($insertdata);
				if ($insert_id) {
					//加载登录信息
					$localuser = $this->User_model->get_user($insert_id);
					$session_data = array(
						'kaixin_keys' => array('session_key' => $kaixin001_session_key),
						'weibo_user' => $weibo_user,
						'userinfo' => $localuser,
						);
					
					$this->session->set_userdata($session_data);
					
					showmessage('登录成功', base_url().'index');
				}else{
					showmessage('登录失败', base_url().'index');
				}
			}else{
				//更新用户信息
				$updatedata = array(
					'lastlogin' => now(),
					'avatar' => $userinfo['logo50'],
					'username' => $weibo_user['name'],
					);
				$this->User_model->update_userinfo_by_uid($localuser['uid'], $updatedata);
				//加载登录信息
				$session_data = array(
					'kaixin_keys' => array('session_key' => $kaixin001_session_key),
					'weibo_user' => $weibo_user,
					'userinfo' => $localuser,
					);
				$this->session->set_userdata($session_data);
				showmessage('登录成功', base_url().'index');
			}
		}else{
			showmessage('登录失败', base_url().'index');			
		}
	}
}
?>